from pylab import sin
import numpy as np
class RungeKuttaSolver():
    
    def __init__(self, function):
	self.function = function
	self.y = 0
    def _do_step(self, t, h = .001 ):
	function = self.function
	y = self.y
	
	K1 = function(t,y)  
        K2 = function(t + (2.0*h/7), y + (2.0*h/7)*K1)
	K3 = function(t + (7.0*h/15), y + h*((77.0*K1/900) + (343.0*K2/900) )) 
	K4 = function(t + (35.0*h/38), y + h*((805.0*K1/1444) - (77175.0*K2/54872) + (97125.0*K3/54872)))
	K5 = function(t + h, y + h*((79.0*K1/490) + (2175.0*K3/3626) + (2166.0*K4/9065)))
	
	tmp = self.y + h*((229.0*K1/1470) + (1125.0*K3/1813) + (13718.0*K4/81585) + (K5/18) )  
	currentValue = tmp
	previousValue = tmp + 1e-10
	while abs(currentValue - previousValue) > 1e-40:
	    previousValue = tmp
	    tmp = self.y + (h/2.0)*(function(t,y) + function(t+h,tmp))
	    currentValue = tmp
	    print (currentValue-previousValue)
	self.y = tmp
	 
    def solver(self, t0= 0, tf= 1, h= .001):
	t = t0
	while t <= tf:
	    self._do_step(t,h)
	    t += h 
	


def sine(t,y):
    return sin(t)


